<?php

namespace App\Models\Flaw;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Eloquent\SoftDeletes;

class Level extends Model
{
    //使用软删除
    use SoftDeletes;
    //关联表
    protected  $table = 'level';
    //填充字段
    protected $fillable = ['level','name','min_reward','max_reward','min_integ','max_integ','term','description'];
    //设置主键
    public $primaryKey = 'id';
    //软删除自动维护字段,不为空时此条数据不显示
    protected $dates = ['deleted_at'];
    // 等级ID
    const SERIOUSID  = 1;  //严重
    const HIGHRISKID = 2;  //高危
    const INRISKID   = 3;  //中危
    const LOWID      = 4;  //低危
    const NOEFFECT   = 5;  //无影响
    /**
     * validate 表单数据验证
     *
     * @param $data
     * @return bool
     */
    public  static function validate($data){
        return $v =Validator::make($data, [
            'level'=>'required|integer',
            'name'=>'required',
            'min_reward'=>'required|integer|min:0',
            'max_reward'=>'required|integer|min:'.++$data['min_reward'],
            'min_integ'=>'required|integer|min:0',
            'max_integ'=>'required|integer|min:'.++$data['min_integ'],
            'term'=>'required|integer|min:0',
            'description'=>'required',
        ],[
                'level.required' => '等级不能为空！',
                'name.required' => '名称不能为空！',
                'min_reward.required' => '最少安全币不能为空！',
                'max_reward.required' => '最大安全币不能为空！',
                'min_integ.required' => '最少积分不能为空！',
                'max_integ.required' => '最大积分不能为空！',
                'term.required' => '请选择期限',
                'description.required' => '请填写描述信息！',
                'level.integer' => '等级必须为整数！',
                'term.integer' => '天数必须为整数！',
                'term.min' => '请输入正确的天数！',
                'min_reward.integer' => '最少安全币必须为整数！',
                'min_reward.min' => '最小安全币需大于0！',
                'max_reward.integer' => '最多安全币必须为整数！',
                'max_reward.min' => '最大安全币需大于'.--$data['min_reward'].'！',
                'min_integ.integer' => '最少积分必须为整数！',
                'min_integ.min' => '最少积分需大于0！',
                'max_integ.integer' => '最多积分必须为整数！',
                'max_integ.min' => '最多积分需大于'.--$data['min_integ'].'！',
            ]
        );
    }

    /**
     * getLevelVuls  获取
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function getLevelVuls()
    {
        return $this->hasMany('App\Models\Flaw\Vuls');
    }

    /**
     * getLevelList 获取等级列表
     *
     * @param bool $condition  是否查询软删除后的数据
     * @return mixed
     */
    public static function getLevelList($condition = true){
        $collection = $condition ? Level::withoutTrashed() : Level::onlyTrashed();
        return $collection->orderby('created_at','desc')->paginate(10);
    }
}
